package com.ruoyi.web.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MyCommodityClasses;
import com.ruoyi.system.service.IMyCommodityClassesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品分类信息Controller
 *
 * @author ruoyi
 * @date 2024-01-13
 */
@RestController
@RequestMapping("/system/classes")
public class MyCommodityClassesController extends BaseController
{
    @Autowired
    private IMyCommodityClassesService myCommodityClassesService;

    @Resource
    private ISysDictDataService dictDataService;
    /**
     * 查询商品分类信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:classes:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCommodityClasses myCommodityClasses)
    {
        startPage();
        List<MyCommodityClasses> list = myCommodityClassesService.selectMyCommodityClassesList(myCommodityClasses);
        return getDataTable(list);
    }

    /**
     * 导出商品分类信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:classes:export')")
    @Log(title = "商品分类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyCommodityClasses myCommodityClasses)
    {
        List<MyCommodityClasses> list = myCommodityClassesService.selectMyCommodityClassesList(myCommodityClasses);
        ExcelUtil<MyCommodityClasses> util = new ExcelUtil<MyCommodityClasses>(MyCommodityClasses.class);
        util.exportExcel(response, list, "商品分类信息数据");
    }

    /**
     * 获取商品分类信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:classes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(myCommodityClassesService.selectMyCommodityClassesById(id));
    }

    /**
     * 新增商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('system:classes:add')")
    @Log(title = "商品分类信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCommodityClasses myCommodityClasses)
    {
        myCommodityClasses.setCreateMan(getUsername());
        // 先做提交，为了让自增的id唯一
        int i = myCommodityClassesService.insertMyCommodityClasses(myCommodityClasses);
        // new 新的对象，把关系作为字典数据存入到ruoyi的系统表，方便管理
        SysDictData sysDictData = new SysDictData();
        // 若顺利返回，则可以进行下一步
        if(i > 0){
            sysDictData.setDictLabel(myCommodityClasses.getCommodityName());
            // 从数据库中得到表，list的长度为 1 仅仅有一个数据
            List<MyCommodityClasses> classesList = myCommodityClassesService.selectMyCommodityClassesList(myCommodityClasses);
            for (MyCommodityClasses commodityClasses : classesList) {
                // 但是数据要迭代，设置字典键值为id值
                sysDictData.setDictValue(commodityClasses.getId().toString());
            }
            //再设置其他信息
            sysDictData.setDictSort(0L);
            sysDictData.setDictType("sys_commodity_classes");
            sysDictData.setListClass("success");
            sysDictData.setStatus("0");
            sysDictData.setCreateBy(getUsername());

        };
        // 方便查看报错信息，如果异常需要抛出，这里起到提示的作用
        // return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        return toAjax(i * dictDataService.insertDictData(sysDictData));
    }

    /**
     * 修改商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('system:classes:edit')")
    @Log(title = "商品分类信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCommodityClasses myCommodityClasses)
    {
        // 修改逻辑，通过id寻找改classes
        MyCommodityClasses classes = myCommodityClassesService.selectMyCommodityClassesById(myCommodityClasses.getId());
        // 设置selectDictName，获取dictDataService的id
        Long aLong = dictDataService.selectDictName("sys_commodity_classes", classes.getCommodityName());
        // 通过id 找到改sysDictData（系统数据，其实是数据字典数据）
        SysDictData sysDictData = dictDataService.selectDictDataById(aLong);
        // 将名字修改。
        sysDictData.setDictLabel(myCommodityClasses.getCommodityName());
        sysDictData.setCreateBy(getUsername());
        // 设置操作人
        myCommodityClasses.setCreateMan(getUsername());
        return toAjax(myCommodityClassesService.updateMyCommodityClasses(myCommodityClasses)*dictDataService.updateDictData(sysDictData));
    }

    /**
     * 删除商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('system:classes:remove')")
    @Log(title = "商品分类信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
//        先获取id，
        MyCommodityClasses myCommodityClasses = myCommodityClassesService.selectMyCommodityClassesById(ids[0]);
//        获得删除的具体类，
        String commodityName = myCommodityClasses.getCommodityName();
//        用自己写的selectDictName找到字典数据的id
        Long aLong = dictDataService.selectDictName("sys_commodity_classes", commodityName);
//      删除字典中的该id
        dictDataService.deleteDictDataByIds(new Long[]{aLong});

        return toAjax(myCommodityClassesService.deleteMyCommodityClassesByIds(ids));
    }
}
